package myapp.com.phoneguard.utils;

import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import org.json.JSONException;
import org.json.JSONObject;

public class SharedPreXML {
    private final String xml="satv_init";
    private SharedPreferences sp=null;

    public SharedPreXML (Context context){
        sp=context.getSharedPreferences(xml, Context.MODE_PRIVATE);
    }

    public String getString (String str){
        return getString(str, "");
    }

    public String getString (String str,String value){
        return sp.getString(str, value);
    }

    public Integer getInt (String str){
        return getInt(str, 0);
    }

    public Integer getInt (String str,int value){
        return sp.getInt(str, value);
    }

    public float getFloat (String str){
        return getFloat(str, 0);
    }

    public float getFloat(String str,float value){
        return sp.getFloat(str, value);
    }

    public boolean getBoolean (String str){
        return getBoolean(str, false);
    }

    public boolean getBoolean (String str,boolean value){
        return sp.getBoolean(str, value);
    }

    public void setString (String str,String value){
        Editor editor = sp.edit();
        editor.putString(str, value);
        editor.commit();
    }

    public void setString (JSONObject object,String ...strs) throws JSONException{
        Editor editor = sp.edit();
        for(String str:strs)
            editor.putString(str, object.getString(str));
        editor.commit();
    }

    public void setInt (String str,int value){
        Editor editor = sp.edit();
        editor.putInt(str, value);
        editor.commit();
    }

    public void setBoolean (String str,boolean value){
        Editor editor = sp.edit();
        editor.putBoolean(str, value);
        editor.commit();
    }

    public void setFloat (String str,float value){
        Editor editor = sp.edit();
        editor.putFloat(str, value);
        editor.commit();
    }

    public void setString (String [] str,String [] value){
        Editor editor = sp.edit();
        for(int i=0;i<str.length ;i++){
            editor.putString(str[i], value[i]);
        }
        editor.commit();
    }

    public void setObject (String [] str,Object [] value){
        Editor editor = sp.edit();
        for(int i=0;i<str.length ;i++){
            if(value[i] instanceof String){
                editor.putString(str[i], value[i].toString());
            }else if(value[i] instanceof integer){
                editor.putInt(str[i], Integer.valueOf(value[i].toString()));
            }else if(value[i] instanceof Boolean){
                editor.putBoolean(str[i], Boolean.getBoolean(value[i].toString()));
            }
        }
        editor.commit();
    }

    public void removeTag(String str){
        Editor editor = sp.edit();
        editor.remove(str);
        editor.commit();
    }
    public void clearData() {
        sp.edit().clear().commit();
    }

}

